package work.MrSun.xyq.controller;

/**
 * @Author MrSun
 * @Date 2023/4/16 15:43
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import work.MrSun.xyq.model.XyqCollect;
import work.MrSun.xyq.model.XyqMessage;
import work.MrSun.xyq.service.XyqCollectService;
import work.MrSun.xyq.service.XyqMessageDetailService;
import work.MrSun.xyq.service.XyqMessageImagesService;
import work.MrSun.xyq.service.XyqUserService;
import work.MrSun.xyq.util.isCollect.IsCollect;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 收藏的实现层
 */
@RestController
public class CollectionController {
    @Autowired
    private XyqCollectService xyqCollectService;

    @Autowired
    private XyqUserService xyqUserService;
    @Autowired
    private XyqMessageImagesService xyqMessageImagesService;

    @Autowired
    private XyqMessageDetailService xyqMessageDetailService;

    /**
     * 收藏的方法
     *
     * @param userId    用户的id
     * @param messageId 收藏信息的id
     * @return 返回值
     */
    @Transactional
    @PostMapping("/addCollection/{userId}/{messageId}")
    public IsCollect addCollection(@PathVariable Integer userId, @PathVariable Integer messageId) {
        return new IsCollect().isTrue(userId, messageId, xyqCollectService, xyqUserService);
    }

    /**
     * 取消收藏
     *
     * @param userId    用户的id
     * @param messageId 消息的id
     * @return
     */
    @Transactional
    @PostMapping("/deleteCollection/{userId}/{messageId}")
    public Integer deleteCollection(@PathVariable Integer userId, @PathVariable Integer messageId) {
        //创建收藏的对象
        XyqCollect xyqCollect = new XyqCollect();
        xyqCollect.setUserId(userId);
        xyqCollect.setMessageId(messageId);
        xyqCollectService.delete(xyqCollect);
        return 200;
    }

    @PostMapping("/getMessage/getAllCollectionMessageByUserId/{userId}/{pageNumber}")
    public List<XyqMessage> getAllCollectionMessageByUserId(@PathVariable Integer userId,
                                                            @PathVariable Integer pageNumber) {
        //进行封装
        PageHelper.startPage(pageNumber, 5);
        //从数据库中查询出该用户所有收藏了哪些消息
        List<XyqCollect> allCollectionMessageByUserId = xyqCollectService.getAllCollectionMessageByUserId(userId);
        //进行封装
        PageInfo<XyqCollect> pageInfo = new PageInfo<>(allCollectionMessageByUserId);
        if (pageInfo.getPageNum() < pageNumber) {
            LinkedList list1 = new LinkedList();
            list1.add(200);
            return list1;
        }
        //获取数据
        List<XyqCollect> list = pageInfo.getList();
        List<XyqMessage> list1 = new ArrayList<XyqMessage>();
        //遍历数据
        for (int i = 0; i < list.size(); i++) {
            //获取每个收藏信息的id
            Integer messageId = list.get(i).getMessageId();
            //获取收藏信息对象
            XyqMessage message = xyqMessageDetailService.getById(messageId);
            list1.add(message);
        }
        //调用getImage方法给每个消息添加图片信息
        return new MessageDetailController().getImage(list1, xyqUserService, xyqMessageImagesService);
    }

    /**
     * 查看用户是不是已经收藏了这个信息
     *
     * @param userId
     * @param messageId
     * @return
     */
    @PostMapping("/addCollection/checkIsCollection/{userId}/{messageId}")
    public Integer checkIsCollection(@PathVariable Integer userId,
                                     @PathVariable Integer messageId) {
        //初始化收藏的对象
        XyqCollect xyqCollect = new XyqCollect();
        //把用户id放在xyqCollect对象中
        xyqCollect.setUserId(userId);
        //把信息的id放在xyqCollect中
        xyqCollect.setMessageId(messageId);
        //调用 xyqCollectService.findCount方法来查询该用户是否已经收藏了这条消息。
        return xyqCollectService.findCount(xyqCollect);
    }
}
